
local IconRender = require("app.widget.renders.IconRender")

local EliteMatchTaskRender = app.mvc.createRenderClass(...)

function EliteMatchTaskRender.create(node)
    local sprite = EliteMatchTaskRender.extend(node or cc.CSLoader:createNode(Res.CrossServerEliteMatchTaskRender))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init()
    return sprite
end

function EliteMatchTaskRender:init(data)
    self.super.init(self, data)
    self:setContentSize(self._ui.Panel:getContentSize())
end

function EliteMatchTaskRender:refresh()
    self:initRenderNode()
    if self._data then
        local task = self._data
        self._ui.bg1:setVisible(true)
        local dropLists = task:getDefaultRewards()
        if dropLists[1] and dropLists[1][1] then
            local iconNode = IconRender.create(self._ui.icon)
            iconNode:setData(dropLists[1][1])
            iconNode:setTipEnable(true)
            iconNode:setVisible(true)
        end
        local taskConf = task:getConfig()
        if task:isRewarded() then -- 已领取
            self._ui.img_lq:setVisible(true)
            self._ui.icon_over:setVisible(true)
            self._ui.tips_1:setVisible(true)
            self._ui.tips_1:setString(L("elitematchLv_11"))
        else
            self._ui.tlt:setVisible(true)
            self._ui.text:setVisible(true)
            if task:isFinished() then -- 可领取
                self._ui.light:setVisible(true)
                self._timeline:gotoFrameAndPlay(0, true)
                self._ui.tlt:setString(L("EliteMatchTip11"))
                self._ui.text:setString(L("EliteMatchTip21"))
                self._ui.Panel:setTouchEnabled(true)
                self._ui.Panel:addTouchEventListener(function(sender, eventType)
                    if eventType == ccui.TouchEventType.ended then
                        -- 领取任务奖励
                        TaskManager:C2S_GetTaskRewardRequest(nil, task:getId())
                    end
                end)
            else -- 未完成
                local doneProgress = task:getDefaultDone()
                local totalProgress = task:getDefaultTotal()
                self._ui.tlt:setString(L(taskConf.name).. "("..doneProgress .. "/" .. totalProgress..")")
                self._ui.text:setString(L(taskConf.target))
            end
        end
    else
        -- 暂未开放
        self._ui.bg2:setVisible(true)
        self._ui.tips_2:setVisible(true)
        self._ui.tips_2:setString(L("EliteMatchTip9"))
    end
end

function EliteMatchTaskRender:initRenderNode()
    self._ui.Panel:setTouchEnabled(false)
    self._ui.bg1:setVisible(false)
    self._ui.bg2:setVisible(false)
    self._ui.icon:setVisible(false)
    self._ui.icon_over:setVisible(false)
    self._ui.img_lq:setVisible(false)
    self._ui.tlt:setVisible(false)
    self._ui.text:setVisible(false)
    self._ui.tips_1:setVisible(false)
    self._ui.tips_2:setVisible(false)
    self._ui.light:setVisible(false)
    self._timeline = self:getTimeline()
    if self._timeline then
        self._timeline:gotoFrameAndPause(0)
    end
end

function EliteMatchTaskRender:getId()
    if self._data then
        return self._data:getId()
    end
end

function EliteMatchTaskRender:setTipsVisible(b)
    self._ui.tips_2:setVisible(b)
end

return EliteMatchTaskRender